<template>
	<view class="container">
		<uni-section title="添加" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" :rules="baseFormrules" :modelValue="baseFormData">
					<uni-forms-item label="姓名" required name="name">
						<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item label="密码" required name="password">
						<uni-easyinput v-model="baseFormData.password" placeholder="请输入年龄" />
					</uni-forms-item>
					<uni-forms-item label="国家" required name="city">
						<uni-data-select v-model="baseFormData.city" :localdata="range"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="性别" required name="sex">
						<uni-data-select v-model="baseFormData.sex" multiple :localdata="sexs" />
					</uni-forms-item>
					<uni-forms-item label="电话" required name="phone">
						<uni-easyinput v-model="baseFormData.phone" placeholder="请输入电话" />
					</uni-forms-item>
					<uni-forms-item label="邮箱" required name="email">
						<uni-easyinput v-model="baseFormData.email" placeholder="请输入邮件" />
					</uni-forms-item>
					<uni-forms-item label="喜好">
						<uni-easyinput type="textarea" v-model="baseFormData.introduction" placeholder="请输入自我介绍" />
					</uni-forms-item>
					<uni-forms-item label="日期">
						<uni-datetime-picker type="datetime" return-type="timestamp" v-model="baseFormData.date" />
					</uni-forms-item>
					<button type="primary" @click="submit('baseForm')">提交</button>
				</uni-forms>
			</view>
		</uni-section>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				// 基础表单数据
				baseFormData: {
					name: '',
					password: '',
					age: '',
					phone: '',
					imp: 1,
					introduction: '',
					sex: '',
					status: '',
					date: this.data
				},
				range: [{
						value: '中国',
						text: '中国'
					},
					{
						value: '美国',
						text: '美国'
					},
					{
						value: '日本',
						text: '日本'
					},
					{
						value: '欧洲',
						text: '欧洲'
					},
				],
				sexs: [{
					text: '男',
					value: '男'
				}, {
					text: '女',
					value: '女'
				}],
				baseFormrules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}]
					},
					city: {
						rules: [{
							required: true,
							errorMessage: '国家不能为空'
						}]
					},
					sex: {
						rules: [{
							required: true,
							errorMessage: '性别不能为空'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '电话不能为空'
						}, {
							format: 'number',
							errorMessage: '电话只能输入数字'
						}]
					},
					email: {
						rules: [{
							required: true,
							errorMessage: '域名不能为空'
						}, {
							format: 'email',
							errorMessage: '域名格式错误'
						}]
					}
				}
			}
		},
		onLoad() {},
		onReady() {
			// 设置自定义表单校验规则，必须在节点渲染完毕后执行
			this.$refs.baseForm.setRules(this.baseFormrules)
		},
		methods: {
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			add() {
				this.dynamicLists.push({
					label: '域名',
					rules: [{
						'required': true,
						errorMessage: '域名项必填'
					}],
					id: Date.now()
				})
			},
			del(id) {
				let index = this.dynamicLists.findIndex(v => v.id === id)
				this.dynamicLists.splice(index, 1)
			},
			submit(ref) {
				this.$refs["baseForm"].validate().then(res => {
					res.imp= 1;
					res.status = '正常';
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>


<style lang="scss">
	.uni-mt-5 {
		margin-top: 5px;
	}
</style>